<template>
	<base-page title="编辑主页" :showBorder="true" backgroundColor="#FFF">
		<view class="mt-20 mx-16 mb-8">
			<text class="text-bold">设置策划人介绍</text>
		</view>
		<view class="mx-16 uni-bg-color radius-8">
			<rich-editor v-model="formData.businessIntroductions" :uploadFilePurpose="1" placeholder="*请您介绍一下自己" />
		</view>
		<template #footer>
			<button class="m-12 h-46 radius-8 bg-primary text-white leading-22 font-normal flex center fs-16 " @click="toSubmit">发布</button>
		</template>
	</base-page>
</template>

<script setup>
	import { onLoad } from '@dcloudio/uni-app'
	import { ref, unref, getCurrentInstance } from 'vue'
	import RichEditor from '../components/rich-editor/rich-editor.vue'
	import { updateHomePageApi } from '@/api/business.js'
	
	const instance = getCurrentInstance()
	
	const merchantInfo = ref()
	const formData = ref({
		id: '',
		businessIntroductions: []
	})

	const toSubmit = async () => {
		const { businessName, businessShortName, ...rest } = formData.value
		updateHomePageApi({
			...rest,
			// businessName: businessShortName // 商家简称修改字段为businessName
		}).then(res => {
			uni.showToast({
				icon: 'none', 
				title: '发布成功'
			})
			uni.$emit('updateHomePage')
			setTimeout(() => uni.navigateBack(), 1500)
		})
	}
	
	const initFormData = (data) => {
		if (data) {
			let introduction = []
			try{
				introduction = JSON.parse(data.introduction)
			}catch(e){
				introduction = [{ type: 1, content: data.introduction }]
			}
			formData.value = { ...data }
			formData.value.businessIntroductions = introduction
		}
	}
	
	onLoad((query) => {
		const eventChannel = instance.ctx.getOpenerEventChannel()
		eventChannel.on('acceptDataFromOpenerPage', (data) => {
			merchantInfo.value = data
			initFormData(data)
		})
	})
</script>
